// OPERATIONS
var ADD_EVENT = "add_event";
var GET_USER_CREATED_EVENTS = "get_user_created_events";
var GET_FUTURE_EVENT_SUGGESTIONS = "get_future_event_suggestions";
var GET_EVENT_DESC = "get_event_desc";

// PARAMETERS
var MODULE_ID = "moduleID";
var TYPE = "type";
var LOCATION = "location";
var START_TIME = "startTime";
var END_TIME = "endTime";
var DESCRIPTION = "description";
var EVENT_ID = "eventID";

$(document).ready(function() {
	printMyEvents();
	printMySuggestedEventsElements();
});

function updateClock() {

	var clientTime = new Date();
	var currentTime = new Date();
	var timeOffset = 8 * 60 * 60 * 1000;
	currentTime.setTime(clientTime.getTime() + timeOffset);

	var currentHours = currentTime.getUTCHours();
	var currentMinutes = currentTime.getUTCMinutes();
	var currentSeconds = currentTime.getUTCSeconds();
	var currentYear = currentTime.getUTCFullYear();
	var currentMonth = currentTime.getUTCMonth();
	var currentDate = currentTime.getUTCDate();
	var currentDay = currentTime.getUTCDay();

	switch (currentMonth) {
	case 0:
		currentMonth = 'Jan';
		break;
	case 1:
		currentMonth = 'Feb';
		break;
	case 2:
		currentMonth = 'Mar';
		break;
	case 3:
		currentMonth = 'Apr';
		break;
	case 4:
		currentMonth = 'May';
		break;
	case 5:
		currentMonth = 'June';
		break;
	case 6:
		currentMonth = 'July';
		break;
	case 7:
		currentMonth = 'Aug';
		break;
	case 8:
		currentMonth = 'Sep';
		break;
	case 9:
		currentMonth = 'Oct';
		break;
	case 10:
		currentMonth = 'Nov';
		break;
	case 11:
		currentMonth = 'Dec';
		break;
	}

	switch (currentDay) {
	case 0:
		currentDay = 'Sunday';
		break;
	case 1:
		currentDay = 'Monday';
		break;
	case 2:
		currentDay = 'Tuesday';
		break;
	case 3:
		currentDay = 'Wednesday';
		break;
	case 4:
		currentDay = 'Thursday';
		break;
	case 5:
		currentDay = 'Friday';
		break;
	case 6:
		currentDay = 'Saturday';
		break;
	}

	currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
	currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

	document.getElementById("clock").innerHTML = currentHours + ":"
			+ currentMinutes + ":" + currentSeconds + " SGT";
	document.getElementById("date").innerHTML = currentDate + " "
			+ currentMonth + " " + currentYear + ",";
	document.getElementById("day").innerHTML = currentDay + ",";
}

window.onload = function() {
	updateClock();
	setInterval(updateClock, 1000);
}

function doAddEvent(moduleID, type, location, startTime, endTime, description) {
	if (xmlhttp) {
		if (moduleID == "") {
			alert("Please enter a module ID!");
		} else {
			xmlhttp.open("POST", "events", false);
			xmlhttp.setRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;");
			xmlhttp.send("operation=" + ADD_EVENT + "&" + MODULE_ID + "="
					+ encodeURIComponent(moduleID) + "&" + TYPE + "="
					+ encodeURIComponent(type) + "&" + LOCATION + "="
					+ encodeURIComponent(location) + "&" + START_TIME + "="
					+ encodeURIComponent(startTime) + "&" + END_TIME + "="
					+ encodeURIComponent(endTime) + "&" + DESCRIPTION + "="
					+ encodeURIComponent(description));
		}
	}
	window.location = "http://sharvyis.appspot.com/home.jsp"
}

function doEditEvent(eventID) {
	// Shakthi Priya's Buggy Code
}

function doDeleteEvent(eventID) {
	if (xmlhttp) {
		if (eventID != "") {
			xmlhttp.open("POST", "events", false);
			xmlhttp.setRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;");
			xmlhttp.send("operation=" + DELETE_EVENT + "&" + EVENT_ID + "="
					+ eventID);
		}
	}
}

function getMyEventList() {
	if (xmlhttp) {
		xmlhttp.open("POST", "events", false);
		xmlhttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;");
		xmlhttp.send("operation=" + GET_USER_CREATED_EVENTS);
	}

	if (xmlhttp.status == 200) {
		var events = xmlhttp.responseXML.getElementsByTagName("EVENTS")[0];
		var eventList = new Array();
		var event;
		var eventID;
		var eventModule;
		var eventType;
		var eventStartTime;

		if (events != null) {
			var eventChildNodesLength = events.childNodes.length;
			for (loop = 0; loop < eventChildNodesLength; loop++) {
				event = events.childNodes[loop];
				eventID = event.getElementsByTagName("EVENTID")[0].firstChild.nodeValue;
				eventModule = event.getElementsByTagName("MODULE")[0].firstChild.nodeValue;
				eventType = event.getElementsByTagName("TYPE")[0].firstChild.nodeValue;
				eventStartTime = event.getElementsByTagName("STARTTIME")[0].firstChild.nodeValue;

				eventList[loop] = {
					eventID : eventID,
					eventModule : eventModule,
					eventType : eventType,
					eventStartTime : eventStartTime
				};
			}
		}

		return eventList;
	}
}

function printMyEvents() {
	$("#myEventsElements").empty();

	var myEventList = new Array();
	myEventList = getMyEventList();

	var count = myEventList.length;
	for (i = 0; i < count; i++) {
		if (myEventList[i].eventID != "") {
			var output = "<li><form><div class=\"btn-toolbar\"><div class=\"btn-group\"><input id=\""
					+ myEventList[i].eventID
					+ "\" type=\"hidden\" value=\""
					+ myEventList[i].eventID
					+ "> <a class=\"btn disabled\" name=\"moduleEvent\" id=\"moduleEvent\"title=\"Module Code\">"
					+ myEventList[i].eventModule
					+ "</a><a class=\"btn disabled\" name=\"typeEvent\" id=\"typeEvent\" title=\"Type\">"
					+ myEventList[i].eventType
					+ "</a> <a class=\"btn disabled\" name=\"startEvent\" id=\"startEvent\" title=\"Start Time\">"
					+ myEventList[i].eventStartTime
					+ "</a></div><div class=\"btn-group pull-right\"><button class=\"btn btn-warning btn-small\" href=\"javascript:doEditEvent(document.getElementById(#"
					+ myEventList[i].eventID
					+ ").value);\" name=\"editEvent\" id=\"editEvent\" title=\"Edit event details\"><i class=\"icon-pencil icon-white\"></i></button><button class=\"btn btn-danger btn-small\" href=\"javascript:doDeleteEvent(document.getElementById(#"
					+ myEventList[i].eventID
					+ ").value);\" name=\"deleteEvent\"id=\"deleteEvent\" title=\"Delete event item\"><i class=\"icon-trash icon-white\"></i></button></div></div></form></li>";
			$("#myEventsElements").append(output);
		}
	}
}

function getSuggestedMyEventList() {
	if (xmlhttp) {
		xmlhttp.open("POST", "events", false);
		xmlhttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;");
		xmlhttp.send("operation=" + GET_FUTURE_EVENT_SUGGESTIONS);
	}

	if (xmlhttp.status == 200) {
		var events = xmlhttp.responseXML.getElementsByTagName("EVENTS")[0];
		var eventList = new Array();
		var event;
		var eventID;
		var eventModule;
		var eventType;
		var eventStartTime;

		if (events != null) {
			var eventChildNodesLength = events.childNodes.length;
			for (loop = 0; loop < eventChildNodesLength; loop++) {
				event = events.childNodes[loop];
				eventID = event.getElementsByTagName("EVENTID")[0].firstChild.nodeValue;
				eventModule = event.getElementsByTagName("MODULE")[0].firstChild.nodeValue;
				eventType = event.getElementsByTagName("TYPE")[0].firstChild.nodeValue;
				eventStartTime = event.getElementsByTagName("STARTTIME")[0].firstChild.nodeValue;

				eventList[loop] = {
					eventID : eventID,
					eventModule : eventModule,
					eventType : eventType,
					eventStartTime : eventStartTime
				};
			}
		}

		return eventList;
	}
}

function printMySuggestedEventsElements() {
	$("#mySuggestedEventsElements").empty();

	var mySuggestedEventList = new Array();
	mySuggestedEventList = getSuggestedMyEventList();

	var count = mySuggestedEventList.length;
	for ( var i = 0; i < count; i++) {
		if (mySuggestedEventList[i].eventID != "") {
			var output = "<li><form><div class=\"btn-toolbar\"><div class=\"btn-group\"><input id=\"suggest "
					+ mySuggestedEventList[i].eventID
					+ " \" type=\"hidden\" value=\" "
					+ mySuggestedEventList[i].eventID
					+ " \"> <a class=\"btn disabled\" name=\"moduleEvent\" id=\"moduleEvent\" title=\"Module Code\"> "
					+ mySuggestedEventList[i].eventModule
					+ " </a> <a class=\"btn disabled\" name=\"locationEvent\" id=\"locationEvent\" title=\"Location\"> "
					+ mySuggestedEventList[i].eventType
					+ " </a> <a class=\"btn disabled\" name=\"startEvent\" id=\"startEvent\" title=\"Start Time\"> "
					+ mySuggestedEventList[i].eventStartTime
					+ " </a></div><div class=\"btn-group pull-right\"><button class=\"btn\" href=\"javascript:doGetDetails(document.getElementById(#suggest "
					+ mySuggestedEventList[i].eventID
					+ " ).value);\" name=\"editEvent\" id=\"getSuggestEvent\" title=\"Get suggest event details\">&raquo;</button></div></div></form></li>";
			$("#mySuggestedEventsElements").append(output);
		}
	}
}

function doGetDetails(eventID) {
	if (xmlhttp) {
		if (eventID != "") {
			xmlhttp.open("POST", "events", false);
			xmlhttp.setRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;");
			xmlhttp.send("operation=" + GET_EVENT_DESC + "&" + EVENT_ID + "="
					+ eventID);
		}
	}

	if (xmlhttp.status == 200) {
		var event = xmlhttp.responseXML.getElementsByTagName("EVENTCHILD")[0];
		var eventID;
		var ownerID;
		var moduleID;
		var type;
		var location;
		var startTime;
		var endTime;
		var description;
		var joined;
		if (eventchild != null) {
			// event = eventchild.childNodes[0];
			eventID = event.getElementsByTagName("EVENTID")[0].nodeValue;
			ownerID = event.getElementsByTagName("OWNER")[0].nodeValue;
			moduleID = event.getElementsByTagName("MODULE")[0].nodeValue;
			type = event.getElementsByTagName("TYPE")[0].nodeValue;
			location = event.getElementsByTagName("LOCATION")[0].nodeValue;
			startTime = event.getElementsByTagName("STARTTIME")[0].nodeValue;
			endTime = event.getElementsByTagName("ENDTIME")[0].firstChild.nodeValue;
			description = event.getElementsByTagName("DESCRIPTION")[0].firstChild.nodeValue;
			joined = event.getElementsByTagName("JOINED")[0].firstChild.nodeValue;
		}
	}
}